import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        //根据是否存在token实现路由跳转
        beforeEnter: (to, from, next) => {
            let user = localStorage.getItem("user")
            //有token则不用登录直接进入页面
            if (user) {
                user = JSON.parse(user)
                if (user.data.status === 1) {
                    next('/adminView')
                } else {
                    next('/clientView')
                }
                //没有token重定向至登录页面
            } else {
                next('/login')
            }
        }
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/client/user/LoginView'),
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('../views/client/user/RegisterView')
    },
    {
        path: '/adminView',
        name: 'adminView',
        component: () => import('../views/admin/AdminView')
    },
    {
        path: '/clientView',
        name: 'clientView',
        component: () => import('../views/client/ClientView')
    }
]

const router = new VueRouter({
    routes
})

export default router
